package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.TUser;
import cn.qf.shop.pojo.qo.TUserQO;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.LoginUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@EnableSwagger2
@RestController
@RequestMapping("/tUser")
@RestControllerAdvice
@Api(tags = "用户业务接口")
public class TUserController extends  BaseController{

    private Logger logger = (Logger) LoggerFactory.getLogger(TUserController.class);
    @Resource
    LoginUserService loginUserService;
    @ApiOperation("用户注册")
    @PostMapping(value = "/register")
    public RespResult register(@RequestBody TUser tUserQO) {

        loginUserService.register(tUserQO);

        return  returnRespWithSuccess(tUserQO);

    }
    //后端管理员用于请求登录的方法
    @ApiOperation("用户登录")
    @PostMapping(value = "/login")
    public RespResult login(@RequestBody TUserQO user,HttpServletRequest request) {
        TUser tUser = loginUserService.login(user);
        request.getSession().setAttribute("loginUser",tUser);
        logger.info("login tUser:{}",tUser );
        return  returnRespWithSuccess(tUser);

    }

}
